package com.life.smile.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

public class FileUploadUtil {

	/**
	 *本地测试
	 */
	/*private static String path = "D:\\Application\\WYueChe\\WYueCheApi\\Images\\";
	private static String IMGURL = "http://192.168.1.100:8062/Images/";*/
	
	/**
	 * 服务器地址
	 */
	private static String path = "D:\\Tomcat\\Images\\";
	private static String IMGURL = "http://114.55.100.37:20000/Images/";
	
	public static String saveXls(MultipartFile file) {
		/**
		 * 保存图片
		 */
		String IMGURLfinal = "";
		if (!file.isEmpty()) {
			String fileName = file.getOriginalFilename();
			System.out.println(fileName);
			//String a = fileName.split("\\.")[1];// 获取文件格式后缀
			String a = fileName.substring(fileName.lastIndexOf("."));
			String filename = UUID.randomUUID() + "." + a;
			IMGURLfinal += IMGURL + filename;
			File files = new File(path + filename); // 新建一个文件
			
			File files1 = new File(path); // 新建一个文件
			if (!files1.exists()) {
				files1.mkdirs();
			}
			BufferedOutputStream stream = null;
			try {
				stream = new BufferedOutputStream(new FileOutputStream(files));
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
		
			int length = 0;
			byte[] buffer = new byte[1024];
			InputStream inputStream;
			try {
				inputStream = file.getInputStream();
				while ((length = inputStream.read(buffer)) != -1) {
					stream.write(buffer, 0, length);
				}
				stream.flush();
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return IMGURLfinal;
		}
		return null;
	}

}
